<?php
$section = 'booking';

$titre = _LB_BOOKING_MANAGEMENT;
$smarty->assign("titre", $titre);

$backlink = Session::getBackLink($section,"list/?");

$act = Request::getIntNumber('act');
$bsID = Request::getIntNumber('sID');
$sesID = Request::getIntNumber('sesID');


if(isset($_GET['sesID']) && isset($_SESSION['adm_bookingData'])  && $_SESSION['adm_bookingData'] != "" && is_array(unserialize($_SESSION['adm_bookingData']))){
	$bookingData = unserialize($_SESSION['adm_bookingData']);
	if(isset($bookingData[$sesID])){
		$smarty->assign('selectedService', $bookingData[$sesID]['SERVICES']);
		$smarty->assign('selectedDATE', $bookingData[$sesID]['DATE']);
		$smarty->assign('selectedTIME', $bookingData[$sesID]['TIME']);
	}
}

if ($act == ACT_VALID_EDIT)
{
	$time = Request::get("TIME");
	$date = Request::get("DATE");
	$date = date_to_date_sql($date);
	$booking_date = $date. " {$time}";
	$techID = Request::getIntNumber("TECHNICIAN");
	$service = $_POST['SERVICE'];
    $arr = array(
		"DATE" =>$date,
		"TIME" =>$time,
		"SERVICES" => $service
	);
	$arrData = array();
	$ind = 0;
	if(isset($_SESSION['adm_bookingData'])){
		$tmp = $_SESSION['adm_bookingData'];
		$arrData = unserialize($tmp);
		$ind = count($arrData);
	}
	if(isset($_GET['sesID']) && isset($_SESSION['adm_bookingData'])  && $_SESSION['adm_bookingData'] != "" && is_array(unserialize($_SESSION['adm_bookingData']))){
		$bookingData = unserialize($_SESSION['adm_bookingData']);
		if(isset($bookingData[$sesID])){
			$arrData[$sesID] = $arr;
		}
		else{
			$arrData[$ind] = $arr;
		}
	}
	else{
		$arrData[$ind] = $arr;
	}
	
	$_SESSION['adm_bookingData'] = serialize($arrData);
	redirect(APPLICATION_URL . "/booking/preview/");
	exit();
}

$service = new Service_Booking();
$serviceData = $service->parseService2Array();

$cfer = new Cfer(array(
	_LB_ACCUEIL => Utils::getHomeLink(),
	_LB_BOOKING_MANAGEMENT => $backlink,
	_LB_ADD_NEW_BOOKING => Utils::getCurrentLink()));
$smarty->assign('cfer', $cfer);
$smarty->assign('backlink', $backlink);
$smarty->assign('sData', $serviceData);
$smarty->assign('bookedSerialize', isset($_SESSION['adm_bookingData'])?$_SESSION['adm_bookingData']:"");
$smarty->display('booking_form.html');
?>